package sz.szsmk.citizencard.util;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import androidx.appcompat.app.AlertDialog;

import com.tbruyelle.rxpermissions.RxPermissions;

import java.lang.ref.WeakReference;

import rx.functions.Action1;


/**
 * Created by yangzhengwen on 2017/10/25.
 */

public class PermissionsUtils {

    public interface OnDoCallback {
        void onDo();

        void onUnDo();
    }

    private static final String TAG = "RxPermissionTest";
    public static void requestReadPermission(final Activity activity, final OnDoCallback onDoCallback) {
        WeakReference<Activity> weakReference = new WeakReference<Activity>(activity);
        final Activity act = weakReference.get();
        RxPermissions.getInstance(act)
                .request(Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Action1<Boolean>() {

                    @Override
                    public void call(Boolean granted) {
                        if (!granted) {  // 至少有一个权限未被授权
                            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                            builder.setTitle("权限提示");
                            builder.setMessage("当前应用缺少相应权限，无法正常使用。如若需要，请单击【授权】按钮行权限授权。");
                            builder.setPositiveButton("授权", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    requestPermissions(activity, onDoCallback);
                                }
                            });
                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    onDoCallback.onUnDo();
                                }
                            });
                            builder.show();
                        } else {
                            if (null != onDoCallback) {
                                onDoCallback.onDo();
                            }
                        }
                    }
                });
    }

    public static void requestPermissions(final Activity activity, final OnDoCallback onDoCallback) {
        WeakReference<Activity> weakReference = new WeakReference<Activity>(activity);
        final Activity act = weakReference.get();
        RxPermissions.getInstance(act)
                .request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Action1<Boolean>() {

                    @Override
                    public void call(Boolean granted) {
                        if (!granted) {  // 至少有一个权限未被授权
                            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                            builder.setTitle("权限提示");
                            builder.setMessage("当前应用缺少相应权限，无法正常使用。如若需要，请单击【授权】按钮行权限授权。");
                            builder.setPositiveButton("授权", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    requestPermissions(activity, onDoCallback);
                                }
                            });
                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    onDoCallback.onUnDo();
                                }
                            });
                            builder.show();
                        } else {
                            if (null != onDoCallback) {
                                onDoCallback.onDo();
                            }
                        }
                    }
                });
    }

    public static void requestPermissions2(final Activity activity, final OnDoCallback onDoCallback) {
        WeakReference<Activity> weakReference = new WeakReference<Activity>(activity);
        final Activity act = weakReference.get();
        RxPermissions.getInstance(act)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Action1<Boolean>() {

                    @Override
                    public void call(Boolean granted) {
                        if (!granted) {  // 至少有一个权限未被授权
                            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                            builder.setTitle("权限提示");
                            builder.setMessage("当前应用缺少相应权限，无法正常使用。如若需要，请单击【授权】按钮行权限授权。");
                            builder.setPositiveButton("授权", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    requestPermissions2(activity, onDoCallback);
                                }
                            });
                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    onDoCallback.onUnDo();
                                }
                            });
                            builder.show();
                        } else {
                            if (null != onDoCallback) {
                                onDoCallback.onDo();
                            }
                        }
                    }
                });
    }

    public static void setLocationPermission(final Activity activity, final OnDoCallback onDoCallback) {
        WeakReference<Activity> weakReference = new WeakReference<>(activity);
        final Activity act = weakReference.get();
        RxPermissions.getInstance(act)
                .request(Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(new Action1<Boolean>() {

                    @Override
                    public void call(Boolean granted) {
                        if (!granted) {  // 至少有一个权限未被授权
                            AlertDialog.Builder builder = new AlertDialog.Builder(act);
                            builder.setTitle("权限提示");
                            builder.setMessage("当前应用缺少定位权限，智能手表功能暂时无法使用。如若需要，请单击【授权】按钮前往设置中心进行权限授权。");
                            builder.setPositiveButton("授权", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
//                                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//                                    Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
//                                    intent.setData(uri);
//                                    activity.startActivity(intent);
                                    setLocationPermission(act, onDoCallback);
                                }
                            });
                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
//                                    setLocationPermission(act, onDoCallback);
                                    onDoCallback.onUnDo();
                                }
                            });
                            builder.show();
                        } else {
                            if (null != onDoCallback) {
                                onDoCallback.onDo();
                            }
                        }
                    }
                });
    }
}
